<?php
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
// +----------------------------------------------------------------------+
// | This source file is a part of iScripts.com Image Gallery v1.0        |
// +----------------------------------------------------------------------+
// | Copyrights Armia Systems, Inc and iScripts.com  2005                |
// | All rights reserved                                                  |
// +----------------------------------------------------------------------+
// | This script may not be distributed, sold, given away for free to     |
// | third party, or used as a part of any internet services such as      |
// | webdesign etc.                                                       |
// +----------------------------------------------------------------------+
include "includes/applicationtop.php";
include "../includes/functions.php";
;

//$paymentids sperated by comma
function CancelPayments($paymentids){
global $tableprefix;
$sql = "SELECT * FROM " . $tableprefix . "settings ";
$res = mysql_query($sql);
$row = mysql_fetch_array($res);
$txtPaypalEmail = $row["paypalemail"];
$txtPaypalSandbox = $row["enablepaypalsandbox"];
$txtAuthorizeLoginId = $row["authorizeloginid"];
$txtAuthorizeTransKey = $row["authorizetranskey"];
$txtAuthorizePassword = $row["authorizepassword"];
$txtAuthorizeEmail = $row["authorizeemail"];
$txt2CheckoutTransKey = $row["checkouttranskey"];
$txtCheckoutDemo = $row["checkoutdemo"];
$txtAuthDemo = $row["authorizedemo"];


$sql="select pm.*,p.planperiods,p.planperiodtype,p.recurring from " . $tableprefix . "payments pm
     left join " . $tableprefix . "userplans up on pm.paymentid=up.paymentid
	 left join " . $tableprefix . "plans p on up.planid=p.planid where pm.paymentid in($paymentids)";
$res = mysql_query($sql);
while($row = mysql_fetch_array($res)){
	  if($row['paymentmethod']=="CC" and $row['status'] !="C" ){
	            $paymentsuccessful=false;
				$path="..";
				$today = date("Y-m-d");
				require "../yourpaycancel.php";
				$rowpaymentid=$row['paymentid'];
				if($txtAuthDemo=="YES"){
			      $transactionid="TEST-".date("Y-m-d-H-s");
			    }
				if($paymentsuccessful==true){
				                       $comments=" Recuring payment canceled for (order : $recurringorderid) on ".$today ." new orderid=$transactionid";
				                       $comments=addslashes($comments);
				                       $sqlupdate = "UPDATE " . $tableprefix . "payments SET status = 'C',comments='$comments' WHERE paymentid = '" . $rowpaymentid . "'";
				                       mysql_query($sqlupdate);
				}else{
									   $paymenterror=" Recuring payment cancelelation  for (order :$recurringorderid ) failed [ $today ] ".addslashes($paymenterror);
									   $sqlupdate = "UPDATE " . $tableprefix . "payments SET status = 'F',comments=concat(comments,'$paymenterror') WHERE paymentid = '" . $rowpaymentid . "'";
				                       mysql_query($sqlupdate);

				}
	  }
}

}



if(!isset($_SESSION["adminauthenticated"]) || $_SESSION["adminauthenticated"] == false){
	echo "<script>window.location.href='login.php';</script>";
	exit;
}

if($begin == ""){
        $begin=0;
        $num=1;
        $numBegin=1;
}
if (get_magic_quotes_gpc()) {
	$txtSearch = stripslashes($_POST["txtSearch"]);
	$cmbSearchType = stripslashes($_POST["cmbSearchType"]);
}else{
	$txtSearch = $_POST["txtSearch"];
	$cmbSearchType =$_POST["cmbSearchType"];
}




if($_POST['postback']=="C"){
	$cancelid=$_POST['cid'];
	$sql="select * from ".$tableprefix."userplans up left join ".$tableprefix."payments
    p on up.paymentid=p.paymentid where p.status='A'  and up.userplanid='".addslashes($cancelid)."' ";
   	$res = mysql_query($sql);
     if(mysql_num_rows($res) >0){
     	$row = mysql_fetch_array($res);
     	$recurringorderid=$row["transactionid"];
     	$planname=$row["planname"];
     	$paymentid=$row["paymentid"];
     	if($row["paymentmethod"]=="CC"){

                   // getting payment info
					$sqlSettings = "SELECT * FROM " . $tableprefix . "settings ";
					$resSettings = mysql_query($sqlSettings);
					$rowSettings = mysql_fetch_array($resSettings);
					$txtAuthorizeLoginId = $rowSettings["authorizeloginid"];
					$txtAuthorizeTransKey = $rowSettings["authorizetranskey"];
					$txtAuthorizePassword = $rowSettings["authorizepassword"];
					$txtAuthorizeEmail = $rowSettings["authorizeemail"];
					$txtAuthDemo = $rowSettings["authorizedemo"];
					$paymentsuccessful = false;
					$path="..";

					require "../yourpaycancel.php";
					if($paymentsuccessful){
                      //SendEmailtoAdminOnCaneclPlan($_SESSION['sessusername'],$planname,"Credit Card");
                      $today = date("Y-m-d");
        			  $sqlupdate = "UPDATE " . $tableprefix . "payments SET status = 'C',comments='Canceled on $today ' WHERE paymentid = '".$paymentid."'";
         			  mysql_query($sqlupdate);
         			  $message=" Subscription canceled successfully ";



					}else{
					   $message =$paymenterror ."<br>";
                       $message .=" Unable to cancel your subscription plan .  ";
					}


	    }else if($row["paymentmethod"]=="PP"){
			   $loc="cancelsubscriptionpp.php?cid=".$row['userplanid'];
	           header("location:$loc");
	           exit;
	   }
   }else{
     		$message= " You cannot cancel this subscription plan .";
   }




}



$qryopt="";
if($txtSearch != ""){
   if($cmbSearchType == "order"){
            $qryopt .= "  WHERE transactionid like '" . addslashes($txtSearch) . "%'";
   }else if($cmbSearchType == "date"){
   		$dt = $txtSearch;
		$arrdt = explode("-",$dt) ;
		$day = $arrdt[1];
		$month = $arrdt[0];
		$year = $arrdt[2];
		$dt = $year."-". $month ."-".$day;
		$qryopt .= "  WHERE  paymentdate like '" . addslashes($dt) . "%'";
   }else if($cmbSearchType == "amount"){
   		//	$st = substr($txtSearch,0,1 );
            $qryopt .= "  WHERE  amountpaid like '" . addslashes($txtSearch) . "%'";
   }else if($cmbSearchType == "status"){
            $qryopt .= "  WHERE  status like '" . addslashes($txtSearch) . "%'";
   }else if($cmbSearchType == "ptype"){
           //if($txtSearch =="PP" or $txtSearch =="CC")

            $qryopt .= "  WHERE  paymentmethod = '" . addslashes($txtSearch) . "'";
   }else if($cmbSearchType == "user"){
           //if($txtSearch =="PP" or $txtSearch =="CC")

            $qryopt .= "  WHERE  username like '%" . addslashes($txtSearch) . "%'";
   }

}



$sql="select * from ".$tableprefix."userplans up left join ".$tableprefix."payments p on up.paymentid=p.paymentid left join ".$tableprefix."users u on up.userid=u.userid ";
if($qryopt !="")
$sql  .= $qryopt;
$sql  .=" order by paymentdate desc" ;
$sess_back= $_SERVER["PHP_SELF"] . "?begin=" . $begin . "&num=" . $num . "&numBegin=" . $numBegin . "&cmbSearchType=" . $cmbSearchType . "&txtSearch=" . $txtSearch;
$_SESSION["gbackurl"] = $sess_back;

$totalrows = mysql_num_rows(mysql_query($sql));

$navigate = pageBrowser($totalrows,10,10,"&cmbSearchType=$cmbSearchType&txtSearch=$txtSearch",$_GET[numBegin],$_GET[start],$_GET[begin],$_GET[num]);
$sql = $sql.$navigate[0];
$rs = mysql_query($sql);
include "includes/header.php";
include "includes/adminmenu.php";
include "includes/bordertop.php";
include "../includes/english.php"
?>

<script language="JavaScript" type="text/JavaScript">
function clickSearch()
{
	document.frmPayments.submit();
}
function clickCalcel(chk) {
			  if(confirm('Are you sure you want to cancel this subscription')) {
				document.frmPayments.postback.value="C";
				document.frmPayments.cid.value=chk;
				document.frmPayments.submit();
			  }

		}
function paymentdetails(upid){

window.open('paymentdetails.php?upid='+upid,'mywindow','width=400,height=300,toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,copyhistory=no,resizable=no,maximize=no')
}
</script>
<p align=left>&nbsp;&nbsp;<b>Subscription Plan </b></p>
<form action="<?=$_SERVER["PHP_SELF"]?>" method="post" name="frmPayments">
   <input type=hidden name="del">
   <input type=hidden name="postback">
   <input type=hidden name="cid">
	<table width="98%"  border="0" cellpadding="0" cellspacing="0" bgcolor="#F5F5F5" align="center">
		<tr>
			<td bgcolor="#F3F3F3">
				<table width="100%"  border="0" cellpadding="2" cellspacing="1" class="maintext">
					<tr align="center" bgcolor="#FFFFFF"><td align="center" colspan="8" class="infomessage"><?=$message?></td></tr>
					<tr bgcolor="#FFFFFF"><td align="right" colspan="8">
						Search ::
						<select name="cmbSearchType" class="selectbox">
							<option value="order"  <? if($cmbSearchType == "status" || $cmbSearchType == ""){ echo("selected"); } ?>>Order#</option>
							<option value="user"  <? if($cmbSearchType == "user"){ echo("selected"); } ?>>User</option>
							<option value="date"  <? if($cmbSearchType == "date"){ echo("selected"); } ?>>Date(mm-dd-yyyy)</option>
							<option value="amount"  <? if($cmbSearchType == "amount"){ echo("selected"); } ?>>Amount</option>
							<option value="status"  <? if($cmbSearchType == "status"){ echo("selected"); } ?>>Status(A/C)</option>
							<option value="ptype"  <? if($cmbSearchType == "ptype"){ echo("selected"); } ?>>Type(PP/CC)</option>

						</select> &nbsp;
				   		<input type="text" name="txtSearch" size="20" maxlength="50" value="<? echo(htmlentities($txtSearch)); ?>"  onKeyPress="if(window.event.keyCode == '13'){ return false; }" class="textbox">
						&nbsp;
						<a href="javascript:clickSearch();"><img src='../images/go.gif'  width="20" height="20" border='0'></a>&nbsp;
						</td>
					</tr>
					 <tr bgcolor="#E0E7E9">
                                <td width="5%">&nbsp;</td>
                                <td width="45%">Plan Name</td>
                                <td width="15%">User</td>
                                <td width="15%">Payment Method</td>
                                <td width="20%">Transaction Id</td>
                                <td>&nbsp;</td>

                        </tr>


				<?
                                                                                        $i = 1;
                                                                                        if(mysql_num_rows($rs) != 0){
                                                                                                while($row = mysql_fetch_array($rs)){
                                                                                                $link ="<a href=\"javascript:paymentdetails(".$row["userplanid"].");\"  class=\"linktext\" style=\"text-decoration:underline;\">";
																								 if($row["paymentmethod"]=="CC"){
																								  $paymentmethod="Credit Card";
																								 }else if($row["paymentmethod"]=="PP"){
																								   $paymentmethod="PayPal";
																								 }
																								 $cancelid=$row["userplanid"];
																								 $cancelstatus=$row["status"];
																								 $activeplan="";
																								 if($row["planstatus"] =="A"){
																								  $activeplan ="<font color=green> [ Active plan ] </font>";
																								 }

																								?>
                                                                                                        <tr class=outertablebgcolor>
                                                                                                                <td  class=maintext><? echo $i; ?></td>
                                                                                                                <td  class=maintext><? echo $link .stripslashes($row["planname"])."</a>".$activeplan; ?></td>
                                                                                                                <td  class=maintext><? echo  stripslashes($row["username"]); ?></td>
                                                                                                                <td  class=maintext><? echo $paymentmethod; ?></td>
                                                                                                                <td  class=maintext><? echo  stripslashes($row["transactionid"]); ?></td>
                                                                                                                <td  class=maintext>
                                                                                                                <?php if($cancelstatus=="A"){?>
																												&nbsp;&nbsp;<a href="javascript:clickCalcel('<?=$cancelid?>');"  class="linktext" style="text-decoration:none;">Cancel</a>
																												<?php }else{
																												  $cancemessage="";
																												  if($row["comments"] !="") $cancemessage=$row["comments"];
																												?>
																												<font color=red><?=$cancemessage?></font>
																												<?php } ?>
																												</td>


                                                                                                        </tr>
                                                                                                <?
                                                                                                        $i++;
                                                                                                }
                                                                                        }else {?>
                                                                                                <!--<tr bgcolor="#FFFFFF"><td colspan="8">No Subscription plans Avalilable!</td></tr>-->
                                                                                        <?}
                                                                                        ?>



					<tr bgcolor="#FFFFFF"><td align="center" colspan="8"><?php echo($navigate[2]); ?></td></tr>
					<tr bgcolor="#FFFFFF"><td align="center" colspan="8">&nbsp;</td></tr>
					<tr bgcolor="#E0E7E9">
						<td colspan="8">&nbsp;</td>
					</tr>
				</table>
			</td>
		</tr>
	</table>
</form>



<?php
include "includes/borderbottom.php";
include "includes/footerlinks.php";
include "includes/footer.php";
?>